package com.yc.damai.damaiother.controller;


import com.yc.damai.bean.SecKillOrder;
import com.yc.damai.damaiother.mapper.DmOrderitemMapper;
import com.yc.damai.damaiother.mapper.DmOrdersMapper;
import com.yc.damai.damaiother.mapper.DmProductMapper;
import com.yc.damai.entity.DmOrderitem;
import com.yc.damai.entity.DmOrders;
import com.yc.damai.entity.DmProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 * 大麦商城-订单表-73 前端控制器
 * </p>
 *
 * @author lpt
 * @since 2024-09-08
 */
@RestController
@RequestMapping("/dmOrders")
@Slf4j
public class DmOrdersController {

    @Autowired
    DmOrdersMapper ordersMapper;
    @Autowired
    DmOrderitemMapper orderitemMapper;
    @Autowired
    DmProductMapper dmProductMapper;
    @RabbitListener(queuesToDeclare = @Queue("secKillQueue"))
    public void saveSecKillOrder(SecKillOrder order){
        // 查询商品
        DmProduct dmProduct = dmProductMapper.selectById(order.getPid());
        // 计算总金额
        Double total = dmProduct.getShopPrice() * order.getCnt();

        DmOrders orders = new DmOrders();
        orders.setUid(order.getUid());
        orders.setTotal(total);
        orders.setState(1);
        orders.setCreatetime(LocalDateTime.now());
        ordersMapper.insert(orders);
        // TODO 设置用户的默认收货地址 (远程调用获取) orders.setAid();
        DmOrderitem item = new DmOrderitem();
        item.setCount(order.getCnt());
        item.setPid(order.getPid());
        item.setTotal(total);
        item.setOid(orders.getId());
        orderitemMapper.insert(item);
        // TODO 给用户发送短信(邮件), 通知用户下订成功!
        log.debug("订单创建成功! " + orders);
    }

}

